const express = require('express');
const path = require('path');
const mongoose = require('mongoose')
const bodyParser = require("body-parser")
// 引入passport  验证token
const passport = require('passport');
var http = require('http');
// DB confing 连接数据库
// 线上数据库
const db = require('./config/key').mongoURL2;
mongoose.connect(db, {
    user: 'root',

    pass: '123456',
    useNewUrlParser: true,
})
    .then(() => {
        console.log(`数据库连接成功${db}`);
    })
    .catch(err => {
        console.log(err);
    })
// 本地数据库
// const db = require('./config/key').mongoURL3;
// mongoose.connect(db, {
//         useNewUrlParser: true,
//     })
//     .then(() => {
//         console.log("数据库连接成功");
//     })
//     .catch(err => {
//         console.log(err);
//     })
// 引入user.js
const users = require('./routers/api/user');
// 引入profiles
const Profile = require('./routers/api/profile')
// 引入Map.js
const Map = require('./routers/api/map')

// 引入ArticleCate
// 创建网站服a务器
const ArticleCate = require('./routers/api/ArticleCate')
// 上传

const app = express();
var server = http.Server(app);
var io = require('socket.io')(server);
var onlieCount = 0;
const sperck = []
// 新用户连接进来时
io.on('connection', function (socket) {
    onlieCount++;
    var a = 0;
    socket.emit('', onlieCount);

    // (() => setInterval(() => {
    //     io.emit('notice', a++);
    // }, 6000))();
    console.log('有一个人进来了,' + '现在有' + onlieCount + '人在线！');
    // 当有用户断开时
    socket.on('change', function (msg) {
        console.log(msg);
    })
    socket.on('emitChat', function (data) {
        sperck.push(data);
        socket.broadcast.emit('notice', sperck);
    })
    socket.on('disconnect', function () {
        onlieCount--;
        console.log(socket.id + '离开了,' + '现在有' + onlieCount + '人在线！');
    });;
    socket.compress('users', function (data) {
        console.log(data);
    })
});
app.use('/static', express.static(path.join(__dirname, 'public')));
// 使用body-parser中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
// :id占位符 前端不需要写id  直接写值
// passport 初始化
// 将passport 传出去
//设置跨域请求
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
app.use(passport.initialize());
require("./config/passport")(passport)

app.get('/static', express.static(path.join(__dirname, 'public/')))
// 用户登录
app.use("/api/users", users);
// 文章列表
app.use("/api/profile", Profile);
// 标签列表
app.use("/api/cate", ArticleCate)
app.use("/api/map", Map)

// 上传
const Upload = require('./routers/api/upload')
app.use('/upload', Upload);
// 端口

const port = process.env.PORT || 8080;
server.listen(port, () => {
    console.log(`服务器启动成功@@!!!${port}`);
})